package biz.noip.strategy.activities;

import javax.microedition.khronos.opengles.GL10;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.TextMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ColorMenuItemDecorator;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.Texture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;

import android.content.Intent;

public class MainMenu extends SimpleBaseGameActivity implements IOnMenuItemClickListener {

	private static final int CAMERA_WIDTH = 480;
	private static final int CAMERA_HEIGHT = 800;

	private Camera mCamera;
	private MenuScene mMenuScene;
	protected Scene mScene;
	private Texture mFontTexture;
	private Font mFont;

	private final static int GAME = 0;
	private final static int OPTIONS = 1;
	private final static int ABOUT = 2;
	private final static int EXIT = 3;

	public EngineOptions onCreateEngineOptions() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		EngineOptions options = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(
				CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
		return options;
	}

	@Override
	protected void onCreateResources() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		mFontTexture = new BitmapTextureAtlas(getTextureManager(), 256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		FontFactory.setAssetBasePath("ttf/");
		this.mFont = FontFactory.createFromAsset(this.getFontManager(), mFontTexture, getAssets(), "Zrnic.ttf", 52,
				true, -1);
		this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
		this.mEngine.getFontManager().loadFont(this.mFont);
	}

	@Override
	protected Scene onCreateScene() {
		this.mMenuScene = new MenuScene(this.mCamera);
		this.mMenuScene.setPosition(10, 10);

		final IMenuItem gameItem = new TextMenuItem(GAME, this.mFont, "GAME", getVertexBufferObjectManager());
		final IMenuItem gameMenu = new ColorMenuItemDecorator(gameItem, Color.BLUE, Color.WHITE);
		gameMenu.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(gameMenu);

		final IMenuItem optionsItem = new TextMenuItem(OPTIONS, this.mFont, "OPTIONS", getVertexBufferObjectManager());
		final IMenuItem optionsMenu = new ColorMenuItemDecorator(optionsItem, Color.BLUE, Color.WHITE);
		gameMenu.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(optionsMenu);
		
		final IMenuItem aboutItem = new TextMenuItem(ABOUT, this.mFont, "ABOUT", getVertexBufferObjectManager());
		final IMenuItem aboutMenu = new ColorMenuItemDecorator(aboutItem, Color.BLUE, Color.WHITE);
		gameMenu.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(aboutMenu);


		final IMenuItem exitItem = new TextMenuItem(EXIT, this.mFont, "EXIT", getVertexBufferObjectManager());
		final IMenuItem exitMenu = new ColorMenuItemDecorator(exitItem, Color.BLUE, Color.WHITE);
		gameMenu.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		this.mMenuScene.addMenuItem(exitMenu);

		this.mMenuScene.buildAnimations();
		this.mMenuScene.setBackgroundEnabled(false);
		this.mMenuScene.setOnMenuItemClickListener(this);

		this.mScene = new Scene();
		this.mScene.setBackground(new Background(Color.BLACK));

		this.mScene.setChildScene(this.mMenuScene, false, true, true);

		return this.mScene;
	}

	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX,
			float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case GAME: {
			Intent intent = new Intent(MainMenu.this, GameActivity.class);
			startActivity(intent);
			return true;
		}
		case OPTIONS: {
			Intent intent = new Intent(MainMenu.this, OptionsActivity.class);
			startActivity(intent);
			return true;
		}
		case ABOUT: {
			Intent intent = new Intent(MainMenu.this, AboutActivity.class);
			startActivity(intent);
			return true;
		}
		case EXIT: {
			finish();
			return true;
		}
		}
		return false;
	}

	protected void onDestroy() {
		super.onDestroy();
		android.os.Process.killProcess(android.os.Process.myPid());
	}

}
